package algorithm_optimal_solution.LinkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;


//两个单链表生成相加链表：方法一使用栈，将两个链表分别放入两个栈，两个栈对应下标的值相加（若大于10需进1位），和存为新的节点。
//方法二利用链表的逆序，两个链表分别逆序，相加的值存为新的节点，求解后将原链表逆序回来。
public class SumLinkedList {

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void twoLinkedListSum(Node head1,Node head2){
        Stack<Node> stack1 = new Stack<>();
        Stack<Node> stack2 = new Stack<>();
        while(head1 != null){
            stack1.push(head1);
            head1 = head1.getNode();
        }
        while(head2 != null){
            stack2.push(head2);
            head2 = head2.getNode();
        }
        int firstNum = 0;
        int secondNum = 0;
        int sumNum = 0;
        int temp = 0;
        Node node = null;
        while(!stack1.isEmpty() || !stack2.isEmpty()){
            firstNum = !stack1.isEmpty() ? stack1.pop().getValue() : 0;
            secondNum = !stack2.isEmpty() ? stack2.pop().getValue() : 0;
            sumNum = firstNum + secondNum + temp;
            temp = sumNum / 10;
            node = new Node(sumNum % 10,node);
        }
        if(temp == 1){
            node = new Node(temp,node);
        }
        StringBuilder sb = new StringBuilder();
        while(node != null){
            sb.append(node.getValue()).append(" ");
            node = node.getNode();
        }
        System.out.print(sb);
    }

    public void twoLinkedListSumByReverse(Node head1,Node head2){
        Node reverseNode1 = reverseLinkedList(head1);
        Node reverseNode2 = reverseLinkedList(head2);
        Node node1 = reverseNode1;
        Node node2 = reverseNode2;
        int firstNum = 0;
        int secondNum = 0;
        int sumNum = 0;
        int temp = 0;
        Node result = null;
        while(node1 != null || node2 != null){
            firstNum = node1!= null ? node1.getValue() : 0;
            secondNum = node2  != null ? node2.getValue() : 0;
            sumNum = firstNum + secondNum + temp;
            temp = sumNum / 10;
            result = new Node(sumNum % 10,result);
            node1 = node1 != null ? node1.getNode() : null;
            node2 = node2 != null ? node2.getNode() : null;
        }
        if(temp ==1){
            result = new Node(temp,result);
        }
        reverseLinkedList(reverseNode1);
        reverseLinkedList(reverseNode2);
        StringBuilder sb  = new StringBuilder();
        while(result != null){
            sb.append(result.getValue()).append(" ");
            result = result.getNode();
        }
        System.out.print(sb);
    }

    private Node reverseLinkedList(Node head){
        Node next;
        Node pre = null;
        while(head != null){
            next = head.getNode();
            head.setNode(pre);
            pre = head;
            head = next;
        }
        return pre;
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        String[] values = scanner.readLine().split(" ");
        int len = Integer.parseInt(values[0]);
        int twoLen = Integer.parseInt(values[1]);
        String[] numbers = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(numbers[i]),node);
        }
        String[] twoNumbers = scanner.readLine().split(" ");
        Node twoNode = null;
        for(int i = twoLen - 1;i >= 0;i--){
            twoNode = new Node(Integer.parseInt(twoNumbers[i]),twoNode);
        }
//        new SumLinkedList().twoLinkedListSum(node,twoNode);
        new SumLinkedList().twoLinkedListSumByReverse(node,twoNode);
    }
}
